﻿#region summary

// Copyright (c)  YISH. All rights reserved.
// Licensed under the MIT License, See License.txt in the project root for license information.

#endregion

using System.Collections.Generic;
using System.Linq;
using NSQL.Sharpen;

namespace NSQL
{
    public class TableSourceCollection : ElementCollection<ITableOrSubquery, Table>, IDeepClone
    {
        public TableSourceCollection(IEnumerable<ITableOrSubquery> elements) : base(elements)
        {
        }
        
        public TableSourceCollection()
        {
        }
        
        public static explicit operator TableSourceCollection(Table val) => new TableSourceCollection {val};

        public int TableCount
        {
            get
            {
                var count = 0;
                foreach (var t in this)
                {
                    if (t is IJoinable i)
                    {
                        while (i is IJoinNode joinNode)
                        {
                            count++;
                            i = joinNode.Base;
                        }
                    }
                    count++;
                    break;
                }
                return count;
            }
        }

        object IDeepClone.DeepCopy()
        {
            return new TableSourceCollection(this.Select(o => o.DeepClone()));
        }
    }
}